<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="text" placeholder="请输入数字" id="tex">
    <input type="button" value="计算" id="btn">
    <div id="box"></div>
</body>
<script>
    var btn = document.getElementById('btn');
    function sum(num) {
        var k = 0;
        if (num % 2 == 0) {
            for (var j = 1; j <= num / 2; j++) {
                k += 1 / (2 * j);
            }
        } else {
            for (var i = 1; i <= (num + 1) / 2; i++) {
                k += 1 / (2 * i - 1);
            }
        }
        return k;
    }

    btn.onclick = function () {
        var tex = document.getElementById('tex').value * 1;
        var box = document.getElementById('box');
        var res = sum(tex);
        box.innerHTML = '计算得出答案为：' + res.toFixed(3);//保留3位小数
    }

</script>

</html>